//备注：拷贝代码请加上作者信息
//作者：王海涛
//邮箱：1126471088@qq.com
//版本：V0.1.0
#include "app_ethernet.h"
#include <string.h>
#include <stdio.h>

#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
#include "../../Driver/Ethernet/Internet/tcp_demo.h"
#include "../../Driver/Ethernet/Internet/udp_demo.h"

//软定时器1句柄
static TimerHandle_t timer1_Handle;

uint8_t dhcp_ok    = 0;        /*dhcp成功获取IP*/
uint32_t dhcp_time = 0;        /*DHCP运行计数*/
volatile uint8_t ntptimer = 0; /*NPT秒计数*/


static void timer1_hook(void* parameter)
{
    dhcp_time++; /*DHCP定时加1S*/
	#ifndef	__NTP_H__
	ntptimer++; /*NTP重试时间加1S*/
	#endif
}

/**	dhcp用到的定时器初始化*/
void dhcp_timer_init(void)
{
    printf("DHCP Start Timer\r\n");
    xTimerStart(timer1_Handle,0);
}
/**	ntp用到的定时器初始化*/
void ntp_timer_init(void)
{
    printf("NTP Start Timer\r\n");
    xTimerStart(timer1_Handle,0);														
}


static void WHT_W5500_Init(void)
{
    uint8_t New_Mac[] = W5500_Defalut_MAC;
    
    WHT_W5500_Driver.WHT_Init(); //初始化MCU相关引脚
    WHT_W5500_Driver.WHT_Reset();//硬复位W5500
    set_w5500_ip(IP_FROM_DEFINE);//配置IP地址
	set_w5500_mac(New_Mac);      //配置MAC地址
	socket_buf_init(txsize, rxsize);//初始化8个Socket的发送接收缓存大小
}


static void WHT_Ethernet_Task(void* pvParameters)
{
	printf("W5500 Demo V1.0 \r\n");		
    WHT_W5500_Init();
    printf("PC TCP Server Mode,W5500 TCP Client Mode\r\n");
	printf("Server IP:%u.%u.%u.%u\r\n",WHT_W5500_Config_Info.rip[0],WHT_W5500_Config_Info.rip[1],WHT_W5500_Config_Info.rip[2],WHT_W5500_Config_Info.rip[3]);
	printf("Port:%u \r\n",WHT_W5500_Config_Info.remote_port);
	printf("Link Succeeded,Server Send Data To W5500,W5500 Send To Server\r\n"); 

    for (; ;)
    {   
        /*TCP_Client 数据回环测试程序*/
        do_tcp_client();
        vTaskDelay(10/portTICK_RATE_MS);
    }
}


void WHT_Ethernet_Install(uint8_t task_priority)
{
    xTaskCreate(WHT_Ethernet_Task, "WHT_Ethernet_Task", 256, NULL, task_priority, NULL);

    timer1_Handle =xTimerCreate("Timer1 code", 1000/portTICK_RATE_MS, pdTRUE, 0, timer1_hook);
}
